Skip to content

fix(types): fix the type definitions for factory models and config, a…#408

Merged
tombeckenham merged 5 commits into
TanStack:mainfrom
liyown:fix/fix-extend-adapter-error-infer-type
Jun 5, 2026
Merged

fix(types): fix the type definitions for factory models and config, a…#408
tombeckenham merged 5 commits into
TanStack:mainfrom
liyown:fix/fix-extend-adapter-error-infer-type

Conversation

@liyown
Copy link
Copy Markdown
Contributor

@liyown liyown commented Mar 29, 2026

🎯 Changes

fix #407

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

Summary by CodeRabbit

  • Bug Fixes

    • Adapter factories now preserve all parameters that follow the model argument (including required positional arguments such as credentials and label metadata), preventing dropped arguments and restoring expected call behavior.
  • Refactor

    • Improved internal typing and factory inference to reliably retain and surface existing parameters after the model without altering runtime behavior.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 29, 2026

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3ba5a9c5-c9cc-4218-bb88-4d140eafb5f1

📥 Commits

Reviewing files that changed from the base of the PR and between b391615 and c345a73.

📒 Files selected for processing (1)
  • packages/ai/tests/extend-adapter.test.ts

📝 Walkthrough

Walkthrough

Rewrites extendAdapter's TypeScript inference to extract model names and preserve all parameters after the model; updates the exported overloads and widens the implementation signature. Adds a changeset documenting the fix and a test asserting required args after the model are preserved.

Changes

Adapter factory parameters preservation fix

Layer / File(s) Summary
Changeset documentation
.changeset/fix-extend-adapter-rest-args.md
A patch changeset documents the fix where extendAdapter now preserves factory parameters that appear after the model argument, including labels and optionality.
Type inference utilities
packages/ai/src/extend-adapter.ts
Adds AnyAdapterFactory, InferFactoryModels, InferAdapterReturn, InferRestArgs, and composes ExtendedFactory to union model names and preserve rest parameters and return types.
Export overloads and implementation signature
packages/ai/src/extend-adapter.ts
Changes exported extendAdapter overload to return ExtendedFactory<TFactory, TDefs> and widens the implementation to AnyAdapterFactory; runtime behavior remains a passthrough to the original factory.
Type-level tests for post-model args
packages/ai/tests/extend-adapter.test.ts
Adds mockChat(model, apiKey, config?) tests that assert the extended factory keeps apiKey required, config optional, and enforces invalid-model type errors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nibbled types with gentle care,
Rest args kept safe from thin air,
Models extended, not a one lost,
Hop—extendAdapter fixed at no cost! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title indicates a fix to type definitions for factory models and config, which directly aligns with the main change to extendAdapter's type inference.
Description check ✅ Passed The description is minimal but references the linked issue #407 and confirms required checklist items were completed.
Linked Issues check ✅ Passed The PR successfully addresses issue #407 by fixing type inference for extendAdapter with chat factories, preserving required parameters after the model argument.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the extendAdapter type definitions to preserve parameters, matching the scope defined in issue #407.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/typescript/ai/src/extend-adapter.ts`:
- Line 113: The generic constraint for InferRestArgs uses an invalid
rest-parameter type; update the constraint on TFactory from using (...args: any)
to a valid rest tuple type such as (...args: any[]) => any so TypeScript accepts
it. Locate the type alias InferRestArgs and change its constraint to TFactory
extends (...args: any[]) => any (and if other similar generic constraints exist
in the same file, apply the same tuple/array rest fix).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 01b3cf38-4277-449f-8500-e5e7a4f22123

📥 Commits

Reviewing files that changed from the base of the PR and between a8a4465 and 0ccbcdf.

📒 Files selected for processing (1)
  • packages/typescript/ai/src/extend-adapter.ts

/**
* Extracts all parameter types after the first parameter from a function.
*/
type InferRestArgs<TFactory extends (...args: any) => any> =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🌐 Web query:

Does TypeScript allow a rest parameter type annotation of any(e.g.,(...args: any) => any), or must it be an array/tuple type like any[]?

💡 Result:

No, TypeScript does not allow a rest parameter type annotation of any like (...args: any) => any. The type annotation for rest parameters must be an array type like any[], a specific array type such as number[], or a tuple type.

Citations:


🏁 Script executed:

head -n 120 packages/typescript/ai/src/extend-adapter.ts | tail -n 20

Repository: TanStack/ai

Length of output: 541


Fix invalid rest-parameter typing in generic constraint.

Line 113 uses (...args: any), but TypeScript requires rest parameters to be array or tuple types (e.g., any[]). This will cause a TypeScript compilation error.

Suggested fix
-type InferRestArgs<TFactory extends (...args: any) => any> =
+type InferRestArgs<TFactory extends (...args: any[]) => any> =
   Parameters<TFactory> extends [any, ...infer Rest] ? Rest : []
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
type InferRestArgs<TFactory extends (...args: any) => any> =
type InferRestArgs<TFactory extends (...args: any[]) => any> =
Parameters<TFactory> extends [any, ...infer Rest] ? Rest : []
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/typescript/ai/src/extend-adapter.ts` at line 113, The generic
constraint for InferRestArgs uses an invalid rest-parameter type; update the
constraint on TFactory from using (...args: any) to a valid rest tuple type such
as (...args: any[]) => any so TypeScript accepts it. Locate the type alias
InferRestArgs and change its constraint to TFactory extends (...args: any[]) =>
any (and if other similar generic constraints exist in the same file, apply the
same tuple/array rest fix).

@tombeckenham tombeckenham self-assigned this May 29, 2026
15367279252qq.com and others added 3 commits June 4, 2026 21:01
…, add regression test

Review follow-ups for TanStack#408:
- InferRestArgs now matches [any?, ...] so factories with an optional
  first parameter don't silently drop their trailing args
- Extract the extended signature into a named ExtendedFactory type and
  restore an explicit return type on extendAdapter (no cast needed)
- Add a regression test for 3-parameter factories like
  createAnthropicChat(model, apiKey, config?) (TanStack#407)
- Add a patch changeset for @tanstack/ai

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@tombeckenham tombeckenham force-pushed the fix/fix-extend-adapter-error-infer-type branch from 0ccbcdf to 865a2e8 Compare June 4, 2026 11:17
Replace any-typed function shapes with a sound AnyAdapterFactory
constraint built on never params / unknown return (parameters are
contravariant, so never accepts every factory). The deliberate
model-union widening now lives in an overload signature instead of an
unchecked assignment, so no cast or any remains anywhere in the file.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@tombeckenham tombeckenham force-pushed the fix/fix-extend-adapter-error-infer-type branch from 3735a3f to b391615 Compare June 4, 2026 11:32
…ories

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@tombeckenham tombeckenham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for supplying the PR for this @liyown. I've locked down the types a bit further to remove type any - which we're actively trying to weed out of the codebase.

@tombeckenham tombeckenham merged commit c0af426 into TanStack:main Jun 5, 2026
3 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is there any way to extend models but with createAnthropicChat rather than anthropicText?

2 participants